package com.vq3.digitalizationsoftwareintegrates.Utils.PermissionControl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Urole_PermissionEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.Mapper.User.Permission_TBMapper;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserFusionService;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserLoginService;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Configuration
public class GetUserPermission {
    @Resource
    UserFusionService userFusionService;
    @Resource
    UserLoginService userLoginService;
    @Resource
    Permission_TBMapper permission_tbMapper;

    public List<Permission_TBEntity> GetPermission(String uname){
        List<Integer> perId = new ArrayList<>();

        UserEntity user = userLoginService.findUser(uname);
        List<User_RoleEntity> user_roleEntities = userFusionService.uidFindURole(user.getUid());
        for (User_RoleEntity user_roleEntity : user_roleEntities) {
            List<Urole_PermissionEntity> urole_permissionEntities = userFusionService.uRidFindURPermission(user_roleEntity.getUroleId());
            for (Urole_PermissionEntity urole_permissionEntity : urole_permissionEntities){
                perId.add(urole_permissionEntity.getUPPermissionId());
            }
        }
        Set<Integer> set = new HashSet<Integer>(perId);
        ArrayList<Integer> integers = new ArrayList<>(set);
        List<Permission_TBEntity> permission_tbEntities = new ArrayList<>();
        for (Integer integer : integers) {
            QueryWrapper<Permission_TBEntity> queryWrapper = new QueryWrapper();
            queryWrapper.eq("permission_id",integer);
            Permission_TBEntity permission_tbEntity = permission_tbMapper.selectOne(queryWrapper);
            permission_tbEntities.add(permission_tbEntity);
        }


        return permission_tbEntities;
    }
}
